package com.xc.shopping_font.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.shopping_font.common.Result;
import com.xc.shopping_font.entity.CollectGoods;
import com.xc.shopping_font.mapper.CollectGoodsMapper;
import com.xc.shopping_font.service.CollectGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/collect")
public class CollectGoodsController {


    @Resource
    CollectGoodsService collectGoodsService;

    @Resource
    CollectGoodsMapper collectGoodsMapper;




    @PostMapping
    public Result addCollect(@RequestBody CollectGoods collectGoods){

        return Result.success(collectGoodsService.save(collectGoods));
    }

    @DeleteMapping("/delcollect")
    public Result delCollect(@RequestParam Long cid,@RequestParam Long productId){

        QueryWrapper<CollectGoods> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("cid",cid);

        queryWrapper.eq("product_id",productId);


        return Result.success( collectGoodsService.remove(queryWrapper));
    }




    @GetMapping("/getGoods")
    public Result getGoods(@RequestParam Long cid){

        QueryWrapper<CollectGoods> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("cid",cid);

        return Result.success(collectGoodsMapper.selectList(queryWrapper));

    }


    @GetMapping("/page")
    public IPage<CollectGoods> page(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
    @RequestParam(defaultValue = "") String uname,@RequestParam Long cid){

        IPage<CollectGoods> page = new Page<>(pageNum,pageSize);

        QueryWrapper<CollectGoods> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("cid",cid);

        if(!"".equals(uname)){
            queryWrapper.like("product_name",uname);
        }

        queryWrapper.orderByAsc("id");

     return collectGoodsService.page(page,queryWrapper);


    }










    @GetMapping
    public Boolean isCollect(@RequestParam Long cid,@RequestParam Long productId){

        QueryWrapper<CollectGoods> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("cid",cid);

        queryWrapper.eq("product_id",productId);

         CollectGoods collectGoods =   collectGoodsMapper.selectOne(queryWrapper);

         if(collectGoods != null){
             return true;
         }

         else {
             return false;
         }

    }



}
